﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;

namespace Controller
{
    public class GetViewInformationController
    {
        public event GetViewInformationEventHandler GetViewInformationCompleted;
        public event GetViewInformationEventHandler GetFullDataOfViewCompleted;


        public void OnGetViewInformationCompleted(GetViewInformationEventArgs e)
        {
            if (GetViewInformationCompleted != null)
            {
                GetViewInformationCompleted(e);
            }
        }

        public void OnGetFullDataOfViewCompleted(GetViewInformationEventArgs e)
        {
            if (GetFullDataOfViewCompleted != null)
            {
                GetFullDataOfViewCompleted(e);
            }
        }
        public void GetViewInformation(ConnectionInformation CI, string ViewName)
        {
            TableInformation result = new TableInformation();
            string message = String.Empty;

            try
            {
                if (CI.DataBaseType == DataBaseType.MSSQLServer)
                {
                    MSSQLDataAccess mSSQLDataAccess = new MSSQLDataAccess();
                    result = mSSQLDataAccess.GetViewInformation(CI, ViewName);
                }
                else if (CI.DataBaseType == DataBaseType.MySQL)
                {
                    MySQLDataAccess mySQLDataAccess = new MySQLDataAccess();
                    result = mySQLDataAccess.GetViewInformation(CI, ViewName);
                }

                OnGetViewInformationCompleted(new GetViewInformationEventArgs(result));
            }

            catch (Exception ex)
            {
                OnGetViewInformationCompleted(new GetViewInformationEventArgs(ex));
            }
        }

        public void GetFullDataOfView(ConnectionInformation CI, ref TableInformation tableInformation)
        {
            try
            {
                if (CI.DataBaseType == DataBaseType.MSSQLServer)
                {
                    MSSQLDataAccess mSSQLDataAccess = new MSSQLDataAccess();
                    mSSQLDataAccess.GetFullData(CI, ref tableInformation);
                }
                if (CI.DataBaseType == DataBaseType.MySQL)
                {
                    MySQLDataAccess mySQLDataAccess = new MySQLDataAccess();
                    mySQLDataAccess.GetFullData(CI, ref tableInformation);
                }

                OnGetFullDataOfViewCompleted(new GetViewInformationEventArgs() { IsFullDataGet = true });
            }
            catch (Exception ex)
            {
                OnGetFullDataOfViewCompleted(new GetViewInformationEventArgs(ex) { IsFullDataGet = false });
            }
        }
    }
}
